package imageProcessing;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.media.jai.PlanarImage;
import javax.swing.JFrame;
import javax.swing.JScrollPane;

import com.sun.media.jai.widget.DisplayJAI;
enum action { save,show};
public class Saver implements ImageListener {
	
	private String _fileName;
	private action _action;
	
	private DisplayJAI _dj;
	private JFrame _frame;
	private JScrollPane _scrollPane;
	
	public Saver() {
		_fileName = "C:\\Temp\\beanboxTempFile.jpg";
		_dj = null;
		_action = action.show;
		_frame = new JFrame( "PipesAndFilters-Output" ); 
	}
	
	@Override
	public void imageChanged(ImageEvent ie) {
		
		PlanarImage img = ie.getImage();
		
		if (_action == action.show) {
			_dj = new DisplayJAI(img);
			if(_scrollPane != null){
				_frame.remove(_scrollPane);
			}
			_scrollPane = new JScrollPane(_dj);
			_scrollPane.setSize(_dj.getWidth(),_dj.getHeight());
			_frame.add(_scrollPane);
			_frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 
			_frame.pack();
			_frame.setVisible(true); 
		} else{
			try {
				ImageIO.write(img, "jpeg", new File(_fileName));
			} catch (IOException e) {
				e.printStackTrace();
			}			
		}
	}

	public void setName(String bildname) {
		_fileName = bildname;
	}

	public String getName() {
		return _fileName;
	}

	public void setOutput(action a) {
		_action = a;
	}

	public action getOutput() {
		return _action;
	}

}
